package com.my.iot.protocol.modbus.tcp;


import com.my.iot.protocol.modbus.tcp.entity.ModbusDTO;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
public class ModbusTcpController {

    @Resource
    private ModbusTcpConnect modbusTcpConnect;

    @Resource
    private ModbusTcpReadAndWrite modbusTcpReadAndWrite;


    @GetMapping("/init")
    public String init() {
      return   modbusTcpConnect.init();
    }

    @GetMapping("/destroy")
    public void destroy() {
        modbusTcpConnect.destroy();
    }

    @GetMapping("/readPoint")
    public Number readPoint(String host, int port, int slaveId, int offset) throws ModbusTransportException, ErrorResponseException {
        return modbusTcpReadAndWrite.readPoint(host, port, slaveId, offset);
    }

    @PostMapping("/readPoints")
    public Map<Integer, Object> readPoints(@RequestBody ModbusDTO dto) throws ModbusTransportException, ErrorResponseException, NoSuchFieldException {
        return modbusTcpReadAndWrite.readPoints(dto);
    }


}
